import React, { Component } from 'react';
import Search from "../../component/Search";
import Table from '../../component/Table';
import Confirm from "../../component/Confirm";
import { formatTime } from '@/utils';

const receiveNum = type => {
    switch (+type){
        case 0: return '全部人员';
        case 1: return '全部学生';
        case 2: return '全部教师';
        default: return '';
    }
};
const receivePersonType = type => {
    var typeStr = '';
    if(type === '1,2,3,4'){
        return '全体人员'
    }else if(type.indexOf('2,3') >= 0){
        typeStr += '全部教师'
    }else if(type.indexOf('1') >= 0){
        typeStr += '全部学生'
    }else if(type.indexOf('4') >= 0){
        typeStr += '后台人员'
    }
    return typeStr
};

class Index extends Component {

    state = {
        searchConfig: {
            columns: [
                {
                    type: 'Input',
                    name: 'headline',
                    placeholder: '标题',
                },
                {
                    type: 'Select',
                    name: 'state',
                    placeholder: '状态',
                    options: [
                        {value: 2, text: '草稿'},
                        {value: 1, text: '已发布'}
                    ]
                },
                {
                    type: 'RangePicker',
                    name: 'releaseTime',
                    range: ['beginDate', 'endDate'],
                    placeholder: ["初始发布时间","结束发布时间"]
                }
            ],
            rightOpt: () => {

            }
        },
        tableConfig: {
            columns: [
                {
                    title: '#',
                    width: 60,
                    fixed: 'left',
                    render: (text, record, index) => {
                        const {current, pageSize} = this.state.tableConfig.pagination;
                        return (current-1) * pageSize + (index+1)
                    }
                },
                {
                    title: '标题',
                    ellipsis: true,
                    dataIndex: 'headline',
                    width: 120
                },
                {
                    title: '内容',
                    ellipsis: true,
                    dataIndex: 'listContent',
                    width: 200
                },
                {
                    title: '接收人员类型',
                    width: 190,
                    render: (text) => {
                        let template = '';
                        if(text.schoolName){
                            template =  text.schoolName + ' ' + text.className + ' '+ receiveNum(text.receiveNum);
                        }else{
                            template = receivePersonType(text.receivePersonType || '')
                        }
                        return template
                    }
                },
                {
                    title: '状态',
                    render: (text) => (text.state === 1 ? '已发布' : '草稿'),
                    width: 80
                },
                {
                    title: '创建人',
                    dataIndex: 'createName',
                    width: 90
                },
                {
                    title: '发布时间',
                    render: (text) => `${formatTime(text.releaseTime, 16)}`,
                    width: 130
                },
                {
                    title: '操作',
                    fixed: 'right',
                    width: 60,
                    render: (text) => {
                        return <Confirm research={this.search}
                                        method="post"
                                        url={'/notice/delete?id='+text.id}
                        ><span className="delete">删除</span></Confirm>
                    }
                },
            ],
            pagination: {
                current: 1,
                pageSize: 15
            },
            params: {
                sortName: "releaseTime",
                sortType: "desc"
            },
            request: {
                method: 'post',
                url: '/notice/list',
                headers: {}
            }
        }
    };

    render() {
        return (
            <div className="table-list">
                <Search config={this.state.searchConfig} search={this.search} />
                <Table config={this.state.tableConfig} onRef={(ref) => this.table = ref } />
            </div>
        )
    }

    // 回调
    search = (searchConfig) => {
        const { tableConfig } = this.state;
        this.setState({
            tableConfig: {
                ...tableConfig,
                pagination: {
                    current: 1,
                    pageSize: 15
                }
            }
        }, () => {
            const { params, pagination } = this.state.tableConfig;
            this.table.getTableList({...params, ...searchConfig}, pagination)
        })
    }
}

export default Index;